import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_test_robots/src/clipboard.dart';

void main() {
  print('=== start clipboard test ===');
  group('ClipboardInteractions Extension Tests', () {
    testWidgets('initializes clipboard', (WidgetTester tester) async {
      // 初始状态应该是 null
      expect(tester.getSimulatedClipboardContent(), isNull);

      // 初始化剪贴板
      tester.simulateClipboard();

      // 等待异步操作完成
      await tester.pumpAndSettle();

      // 初始化后，未set，内容应该是 null
      expect(tester.getSimulatedClipboardContent(), isNull);
    });

    testWidgets('sets clipboard text', (WidgetTester tester) async {
      // 初始化剪贴板
      tester.simulateClipboard();
      await tester.pumpAndSettle();

      const testText = 'Hello, Clipboard!';

      // 设置剪贴板内容
      await tester.setSimulatedClipboardContent(testText);
      await tester.pumpAndSettle();

      // 验证内容是否正确设置
      expect(tester.getSimulatedClipboardContent(), testText);
    });

    testWidgets('get correct text after setting', (WidgetTester tester) async {
      // 初始化剪贴板
      tester.simulateClipboard();
      await tester.pumpAndSettle();

      const testText = 'Test Text!@#';
      
      // 设置剪贴板内容
      await tester.setSimulatedClipboardContent(testText);
      await tester.pumpAndSettle();

      // 验证内容是否正确
      expect(tester.getSimulatedClipboardContent(), testText);
    });

    testWidgets('clears clipboard content', (WidgetTester tester) async {
      // 初始化剪贴板
      tester.simulateClipboard();
      await tester.pumpAndSettle();

      const testText = 'Text to be cleared';
      
      // 设置剪贴板内容
      await tester.setSimulatedClipboardContent(testText);
      await tester.pumpAndSettle();
      
      // 验证内容已设置
      expect(tester.getSimulatedClipboardContent(), testText);
      
      // 清除剪贴板
      tester.clearSimulatedClipboard();
      await tester.pumpAndSettle();

      // 验证内容已被清除
      expect(tester.getSimulatedClipboardContent(), isNull);
    });

    testWidgets('set clipboard content without initialization', (WidgetTester tester) async {
      // 不先调用 simulateClipboard，直接设置内容
      const testText = 'Direct set without init';
      
      await tester.setSimulatedClipboardContent(testText);
      await tester.pumpAndSettle();
      
      // 验证内容已设置
      expect(tester.getSimulatedClipboardContent(), testText);
    });

    testWidgets('multiple set operations work correctly', (WidgetTester tester) async {
      // 初始化剪贴板
      tester.simulateClipboard();
      await tester.pumpAndSettle();

      // 第一次设置
      await tester.setSimulatedClipboardContent('First Text');
      await tester.pumpAndSettle();
      expect(tester.getSimulatedClipboardContent(), 'First Text');
      
      // 第二次设置，应该覆盖第一次
      await tester.setSimulatedClipboardContent('Second Text');
      await tester.pumpAndSettle();
      expect(tester.getSimulatedClipboardContent(), 'Second Text');
      
      // 第三次设置
      await tester.setSimulatedClipboardContent('Third Text');
      await tester.pumpAndSettle();
      expect(tester.getSimulatedClipboardContent(), 'Third Text');
    });

    testWidgets('clear and reset works correctly', (WidgetTester tester) async {
      // 初始化剪贴板并设置内容
      tester.simulateClipboard();
      await tester.pumpAndSettle();
      await tester.setSimulatedClipboardContent('Initial Text');
      await tester.pumpAndSettle();
      expect(tester.getSimulatedClipboardContent(), 'Initial Text');
      
      // 清除剪贴板
      tester.clearSimulatedClipboard();
      await tester.pumpAndSettle();
      expect(tester.getSimulatedClipboardContent(), isNull);
      
      // 重新初始化并设置新内容
      tester.simulateClipboard();
      await tester.pumpAndSettle();
      await tester.setSimulatedClipboardContent('New Text');
      await tester.pumpAndSettle();
      expect(tester.getSimulatedClipboardContent(), 'New Text');
    });
  });
}
